<template>
  <div id="detail">
    <detailnav @itemclick="itemclick" ref="nav"></detailnav>

    <scroll class="wrapper" ref="scroll" :pullUpLoad="false" @content="contentScroll" :currentprobetype="3" @showback="showback">
      <!--  <ul>
        	<li v-for="item in $store.state.cartList">{{item}}</li>
        </ul> -->
        <detail-swiper :banners="banners"/>

        <detailsitem :goods="goods"/>

        <detail-shop :shop="shop"/>
        <detail-info :detailInfo="detailInfo" @imageload="imageLoadFinish"></detail-info>
         <detailitemparams :GoodsParams="GoodsParams" ref="params"></detailitemparams>

         <detail-comment :CommentInfo="CommentInfo" ref="comment"></detail-comment>


         <goodlist :goods="recommends" ref="recommend"></goodlist>

    </scroll>
    <backtotop @click.native="backtotop" v-show="isShow"></backtotop>
    <detail-tabbar @addToCart="addToCart"></detail-tabbar>
  </div>
</template>

<script>
  import {getDetail,getRecommend,Goods,Shop,Detail,GoodsParams} from "../../network/detail.js"
  import {debounce} from "../../common/utils/utils.js"
  import {mapActions} from 'vuex'

  import Scroll from "../../components/common/scroll/scroll.vue"
  import goodlist from "../../components/content/goods/GoodsList/GoodsList.vue"
  import {itemimglistener} from "../../common/itemimglistener/itemimglistener.js"

  import detailnav from "./childComps/detailNav.vue"
  import detailSwiper from "./childComps/detailSwiper.vue"
  import detailsitem from "./childComps/detailsitem.vue"
  import detailShop from "./childComps/detailShop.vue"
  import detailInfo from "./childComps/detailInfo.vue"
  import detailitemparams from "./childComps/detailItemParams.vue"
  import detailComment from "./childComps/detailComment.vue"
  import detailTabbar from "./childComps/detailTabbar.vue"
  import backtotop from "../../components/common/backtotop/backtotop.vue"
  export default{
    name:"Detail",
    data:function(){
      return{
        iid:null,
        banners:null,
        goods:{},
        shop:{},
        detailInfo:{},
        GoodsParams:{},
        CommentInfo:{},
        recommends:[],
        tonavposition:[],
        toNav:null,
        refresh:null,
        currentIndex :0,
        isShow:false
      }
    },
    components:{
      detailnav,detailSwiper,detailsitem,detailShop,detailInfo,detailitemparams,detailComment,detailTabbar,
      Scroll,goodlist,backtotop
    },
  created() {
    this.refresh = debounce(()=>{
      this.$refs.scroll.refresh()
    })
    this.toNav = debounce(()=>{
      this.tonavposition =[]
      this.tonavposition.push(0)
      this.tonavposition.push(this.$refs.params.$el.offsetTop)
      this.tonavposition.push(this.$refs.comment.$el.offsetTop )
      this.tonavposition.push(this.$refs.recommend.$el.offsetTop )
      console.log(this.tonavposition)
    },500)
    // 数据请求类的操作


    this.iid = this.$route.params.iid ? this.$route.params.iid :this.$route.params.shop_id
    getDetail(this.iid).then(res=>{
      console.log(res)
      this.banners = res.result.itemInfo.topImages
      const data = res.result
      this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)
      console.log(this.goods)
      const shop = res.result.shopInfo
      this.shop = new Shop(shop)
      console.log(shop)
      const detail = res.result.detailInfo
      this.detailInfo =new Detail(detail)
      console.log(this.detailInfo)
      this.GoodsParams = new GoodsParams(res.result.itemParams.info,res.result.itemParams.rule)
      console.log(this.GoodsParams)
      if(res.result.rate.cRate !==0){
        this.CommentInfo = res.result.rate.list[0]
        console.log(this.CommentInfo)
        getRecommend().then(res=>{
          this.recommends = res.data.list
          console.log(this.recommends)
        })

      }
    })
  },
  destroyed(){
    this.$bus.$off('imgLoad',this.itemimglistener)
  },

  mixins:[itemimglistener],



methods:{
  imageLoadFinish(){
    this.refresh(),
    this.toNav()

  },
  showback(position){
    this.isShow = -position.y > 1000
  },
  backtotop(){
    this.$refs.scroll.scrollTo(0,0,500)
  },
  itemclick(index){
    this.$refs.scroll.scrollTo(0,-this.tonavposition[index],300)
  },
  contentScroll(position){
    // console.log(-position.y)
    for(let i = 0;i<this.tonavposition.length;i++){
     if(this.currentIndex !=i){
       if((i<this.tonavposition.length -1 &&-position.y>this.tonavposition[i] && -position.y<this.tonavposition[i + 1])
       ||(i ===this.tonavposition.length - 1 && -position.y >this.tonavposition[i] -100)){
          this.currentIndex = i
          this.$refs.nav.currentindex = this.currentIndex
          // console.log(this.currentIndex)
       }
     }
    }
  },
  // 映射actions里的方法addCart
  ...mapActions(['addCart']),
  addToCart(){
    const product = {}
    product.img = this.banners[0];
    product.title = this.goods.title;
    product.desc = this.goods.desc;
    product.price = this.goods.realPrice;
    product.iid = this.iid
    product.checked = false
    console.log(product)
    // this.$store.commit('addCart',product)
    // 在actions的方法里返回一个promise
    // this.$store.dispatch('addCart',product).then(res=>{
    //   console.log(res)
    // })
    // 也可以使用mapActions
    this.addCart(product)
      // console.log(res)

  }
}
  }
</script>

<style scoped="scoped">
  #detail{
    height: 100vh;

  }
.wrapper{
  height: calc(100% - 44px - 58px);
  overflow: hidden;
}
</style>
